﻿class Solution {
    public List<String> generateParenthesis(int n) {
List<String> list =new ArrayList<String>();
dfs(list,n,n,"");
return list;
    }  
    public void dfs(List<String> list,int left,int right,String temp){
        if(left==0&&right==0){
            list.add(temp);
            return;
        }
        if(left<0){
            return;
        }
        if(right<left){//如果剩余右括号小于左括号，肯定不对
            return;
        }
        dfs(list,left-1,right,temp+"(");
         dfs(list,left,right-1,temp+")");

    }
}